Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pool.gno #403

Merged
merged 28 commits into from
Dec 10, 2024
Merged

refactor: pool.gno #403

merged 28 commits into from
Dec 10, 2024

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Nov 28, 2024

  • mainly for the Swap function
  • add test for validating entry functions

@notJoon notJoon added refactoring pool Pool contract related tasks labels Nov 28, 2024
@notJoon notJoon self-assigned this Nov 28, 2024
@dongwon8247
Copy link
Member

You can extract repeated logic into a function.

  1. This pattern, I see in Mint and Burn.
liquidityAmount := u256.MustFromDecimal(_liquidityAmount)
if liquidityAmount.IsZero() {
    panic(addDetailToError(
        errZeroLiquidity,
        ufmt.Sprintf("pool.gno__Mint() || liquidityAmount == 0"),
    ))
}

  1. This is repeated in Mint, Burn and Swap.
if common.GetLimitCaller() {
    caller := std.PrevRealm().Addr()
    if err := common.PositionOnly(caller); err != nil {
        panic(addDetailToError(
            errNoPermission,
            ufmt.Sprintf("pool.gno__Mint() || only position(%s) can call pool mint(), called from %s", consts.POSITION_ADDR, caller.String()),
        ))
    }
}
  1. Across all functions
pool := GetPool(token0Path, token1Path, fee)
  1. This was used Swap.
if _amountSpecified == "0" {
    panic(addDetailToError(
        errInvalidSwapAmount,
        ufmt.Sprintf("pool.gno__Swap() || amountSpecified == 0"),
    ))
}

Can you also try to break down the Mint, Burn, Collect and Swap functions as small as possible?
I know it's a WIP, but it needs more unit tests. Add them.

@notJoon
Copy link
Member Author

notJoon commented Dec 3, 2024

@dongwon8247

You can extract repeated logic into a function.

1. This pattern, I see in `Mint` and `Burn`.

2. This is repeated in `Mint`, `Burn` and `Swap`.

I agree with integrating panic condition checks like 1 and 2 into a single function. However, it seems difficult to integrate them immediately since each function currently outputs different error messages. We plan to improve how we manage error messages and error codes, and I think it would be better to handle this integration when the error messages are reorganized at that time.

Proposed Idea: https://www.notion.so/Contract-Refactoring-14317eb23ada80c4bdd6c1ca7b2349bd?pvs=4#15017eb23ada802abf80d47c9e90eea7

3. Across all functions
pool := GetPool(token0Path, token1Path, fee)

...
Can you also try to break down the Mint, Burn, Collect and Swap functions as small as possible? I know it's a WIP, but it needs more unit tests. Add them.

While the line calling the GetPool function is clearly repeated, extracting it into a separate function seems unnecessary. I believe this could actually reduce readability by making it harder to locate where GetPool is being called for each function.

Regarding this, I haven't made many modifications to the entry functions except for the Swap function. This is because the structure and implementation of these functions has appropriate readability 1, and there's no reusable logic, so I determined it unnecessary as increasing the number of functions could potentially lead to cognitive overhead.

However, as you mentioned, the Swap function had state changes and calculations mixed together, so I applied refactoring to separate them as much as possible for testing purposes. please check this commit: 0b6d9d4

And for the test, don't worry

if _amountSpecified == "0" {
    panic(addDetailToError(
        errInvalidSwapAmount,
        ufmt.Sprintf("pool.gno__Swap() || amountSpecified == 0"),
    ))
}

The logic for checking if amountSpecified is "0" appears to be a pattern found in other contracts as well, so it must be extract it into a common package.

Footnotes

  1. Here, readability means not only being able to grasp at a glance but also being able to understand how the calculation flows.

@notJoon notJoon requested review from onlyhyde and r3v4s December 4, 2024 04:12
@notJoon notJoon marked this pull request as ready for review December 4, 2024 04:13
pool/pool.gno Show resolved Hide resolved
pool/_RPC_dry.gno Outdated Show resolved Hide resolved
pool/pool.gno Outdated Show resolved Hide resolved
r3v4s
r3v4s previously approved these changes Dec 8, 2024
Copy link
Member

@r3v4s r3v4s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve onlyhide'd comment about test case, other than that LGTM 👍

@notJoon
Copy link
Member Author

notJoon commented Dec 9, 2024

There is no test code for position_update.gno.

@onlyhyde
added 19f1a8e

@notJoon notJoon requested a review from r3v4s December 9, 2024 06:03
pool/_RPC_dry.gno Outdated Show resolved Hide resolved
r3v4s
r3v4s previously approved these changes Dec 9, 2024
pool/_RPC_dry.gno Outdated Show resolved Hide resolved
pool/position_modify.gno Show resolved Hide resolved
r3v4s
r3v4s previously approved these changes Dec 9, 2024
Copy link
Member

@onlyhyde onlyhyde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onlyhyde onlyhyde requested a review from dongwon8247 December 10, 2024 06:02
@r3v4s r3v4s self-requested a review December 10, 2024 06:05
@onlyhyde onlyhyde merged commit 751edf8 into main Dec 10, 2024
2 of 3 checks passed
@onlyhyde onlyhyde deleted the refactor-pool branch December 10, 2024 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pool Pool contract related tasks refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants